<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Contact;

/**
 * ContactSearch represents the model behind the search form of `common\models\Contact`.
 */
class ContactSearch extends Contact
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'cCategory', 'type', 'cLevel', 'disable', 'isDelete', 'admin_id', 'created_at', 'updated_at'], 'integer'],
            [['name', 'number', 'cCategoryName', 'beginDate', 'remark', 'linkMans', 'contact', 'cLevelName', 'pinYin'], 'safe'],
            [['taxRate', 'amount', 'periodMoney', 'difMoney'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Contact::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'cCategory' => $this->cCategory,
            'taxRate' => $this->taxRate,
            'amount' => $this->amount,
            'periodMoney' => $this->periodMoney,
            'difMoney' => $this->difMoney,
            'beginDate' => $this->beginDate,
            'type' => $this->type,
            'cLevel' => $this->cLevel,
            'disable' => $this->disable,
            'isDelete' => $this->isDelete,
            'admin_id' => $this->admin_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'number', $this->number])
            ->andFilterWhere(['like', 'cCategoryName', $this->cCategoryName])
            ->andFilterWhere(['like', 'remark', $this->remark])
            ->andFilterWhere(['like', 'linkMans', $this->linkMans])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'cLevelName', $this->cLevelName])
            ->andFilterWhere(['like', 'pinYin', $this->pinYin]);

        return $dataProvider;
    }
}
